var Util = require('../../utils/util.js');
const Api = require('../../api/api.js')
var app = getApp()
Page({
  data: {
    secnicname: '',
    orderInfo: "",
    sys_code: '',
    ticketId: '',
    bookday: '',
    minbookday: '',
    maxbookday: '',
    totalPrice: 0,
    price: 0,
    name: '',
    phone: '',
    idcard: '',
    calendarPriceCount: '',
    response: {

    },
    num: {
      min: 1,
      value: 1,
      max: 1000
    },
    yd_show: true,
    calendar: [],
    ticketTags:[]
  },
  onLoad: function (params) {
    //初始化表单验证
    this.WxValidate = app.WxValidate(
      {
        name: {
          required: true,
          minlength: 2,
          maxlength: 10,
        },
        phone: {
          required: true,
          tel: true,
        },
        idcard: {
          required: true,
          idcard: true
        }
      }
      , {
        name: {
          required: '请填写您的姓名',
        },
        phone: {
          required: '请填写您的手机号',
        },
        idcard: {
          required: '请填写您的身份证号',
        }
      }
    )

    this.setData({
      sys_code: params.sys_code,
      ticketId: params.ticketId,
    })
    console.log("add order" + this.data.ticketId)
    this.bindData();
  }
  , onShow: function () {
    console.log('addorder onShow')
  },

  onReady: function () {
    console.log('addorder onReady')
    // Do something when page ready.
  },

  //初始化数据
  bindData: function () {
    var pagethis = this
    // console.log('addorder: ')
    Api.getOrderInfo({
      data: app.addParams({
        ticketId: pagethis.data.ticketId
      }),
      success: (res) => {
        console.log('+++++++++++++++景区下单详情+++++++++++++++')
        console.log(res)
        let tprice = Util.getTotalPrice(pagethis.data.num.value, res.data.data.ticket.price);
        let _ticketTags = []
        if (res.data.data.ticket.tags){
          let tagString = res.data.data.ticket.tags.split(',')
          let _m = app.ticketTags
          tagString.forEach((item)=>{
            _ticketTags.push(_m[item])
          })
        }
        pagethis.setData({
          response: res.data,
          minbookday: res.data.data.earlyBookDay,
          bookday: res.data.data.earlyBookDay,
          totalprice: tprice,
          price: res.data.data.ticket.price,
          secnicname: res.data.data.ticket.name,
          ticketTags: _ticketTags
        })
        pagethis.getDayPrice();
      }
    })
  },

  // 控制加
  addCount: function (event) {
    let _num = this.data.num
    if (_num.value > _num.max) return;
    _num.value += 1;
    let tprice = Util.getTotalPrice(_num.value, this.data.response.data.ticket.price);
    this.setData({
      num: _num,
      totalprice: tprice
    })
  },
  // 控制减
  minusCount: function (event) {
    let _num = this.data.num
    if (_num.value <= _num.min) return;
    _num.value -= 1;
    let tprice = Util.getTotalPrice(_num.value, this.data.response.data.ticket.price);
    this.setData({
      num: _num,
      totalprice: tprice
    })
  },
  //拖动
  sliderchange: function (e) {
    let _data = e.detail.value, _num = this.data.num;
    _num.value = _data;
    let tprice = Util.getTotalPrice(_data, this.data.response.data.ticket.price);
    this.setData({
      num: _num,
      totalprice: tprice
    })
  },

  //表单验证
  formSubmit: function (e) {
    //提交错误描述
    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList[0]
      // `${error.param} : ${error.msg} `
      wx.showModal({
        showCancel: false,
        title: '提交错误',
        content: `${error.msg} `
      })
      return false
    }
    this.setData({
      name: e.detail.value.name,
      phone: e.detail.value.phone,
      idcard: e.detail.value.idcard
    })
    this.goToBuy();
  },
  //选择日期重置价格
  getDayPrice: function () {
    var pagethis = this
    Api.getDayPrice({
      data: app.addParams({
        ticketId: pagethis.data.ticketId
      }),
      success: (res) => {
        console.log('+++++++++++++++获取日历数据+++++++++++++++')
        console.log(res)
        if (res.data.data.length > 0) {
          pagethis.setData({
            calendar: res.data.data,
            bookday: res.data.data[0].date,
            minbookday: res.data.data[0].date
          })
        }

      },
      fail: (res) => {
        wx.showModal({
          showCancel: false,
          title: '出错啦',
          content: `${error.msg} `
        })
      }
    })
  },
  goToBuy: function () {
    var pagethis = this
    Api.goToPaying({
      data: app.addParams({
        ticketId: pagethis.data.ticketId,
        'orderInfo.occDate': pagethis.data.bookday,
        'orderInfo.linkName': pagethis.data.name,
        'orderInfo.linkIdcard': pagethis.data.idcard,
        'orderInfo.linkMobile': pagethis.data.phone,
        amount: pagethis.data.num.value
      }),
      success: (res) => {
        if (res.data.result == true) {
          wx.redirectTo({
            url: '../gopay/gopay?orderInfo=' + res.data.data.orderInfoId + '&ticketId=' + pagethis.data.ticketId
          })
        } else {
          wx.showModal({
            title: '下单失败',
            content: res.data.msg || '',
            success: function (res) {

            }
          })

        }
      }
    })
  },
  //关闭富文本弹框
  cancelText: function () {
    this.setData({
      yd_show: true
    })
  },
  // 打开富文本弹框
  openMaskContent: function (e) {
    this.setData({
      yd_show: false
    })
  },
  //切换日历
  changeCalendar: function (e) {
    console.log('+++++++++++++++++++++切换日历+++++++++++++++++++++++')
    let that = this;
    wx.navigateTo({
      url: '../calendar/calendar?ticketId=' + that.data.ticketId,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })  
  }
})